/*
 * @Description: 车票订单创建提交页
 * @Author: shuaishuai.wang
 * @Date: 2019-08-21 09:42:50
 * @Last Modified by: ziwei.ma
 * @Last Modified time: 2019-10-31 20:43:05
 */
<template>
  <div
    class="create_main fontsize32"
    v-wechat-title="$route.meta.title"
  >
    <loading :state="loadingState"></loading>
    <div class="create_area">
      <div class="ticket_info part">
        <div class="line_name flex-h flex-hsb">
          <h3 class="fontsize32">{{routeName}}</h3>
          <p class="fontsize28">{{this.order.OVER_TIME | overTimeFilters}}</p>
        </div>
        <div class="line_main">
          <div class="change_icon">
            <i class="iconfont icon-bus fontsize48"></i>
          </div>
          <div class="line_info">
            <div class="station">{{this.order.START_STATION}}</div>
            <div
              class="station_list"
              @click="toggleList = !toggleList"
            >
              <div
                class="station_num"
                :style="{height: (toggleList ? 0 : numHeight)}"
              >
                <span>{{this.order.VIA_STATION_COUNT | stationFilters}}</span>
                <i
                  class="iconfont icon-unflod"
                  v-show="this.order.VIA_STATION_COUNT!==0"
                ></i>
              </div>
              <div
                class="station_way"
                :style="{height: (toggleList ? wayHeight : 0)}"
              >
                <p
                  v-for="(station, index) in this.order.VIA_STATION_LIST"
                  :key="index"
                >{{station}}</p>
              </div>
            </div>
            <div class="station">{{this.order.END_STATION}}</div>
            <div class="long_string">
              <i class="iconfont icon-circle start"></i>
              <i class="iconfont icon-circle end"></i>
            </div>
          </div>
        </div>
        <div class="ticket_time fontsize28">
          <div v-if="order.TICKET_DATE_TYPE===1"><span class="fontsize28">班次：</span>{{this.order.DEPART_TIME}}</div>
          <div
            v-else
            @click="showStartPlan"
          ><span class="fontsize28">发车计划 <i class="baseColorNoAcitve">></i></span></div>
        </div>
        <div class="tag">
          <img :src="order.TICKET_DATE_TYPE | showTheTag()">
        </div>
      </div>
      <div class="passenger part">
        <div class="num"><span>乘车人数：</span>{{this.order.TAKE_BUS_PEOPLE_COUNT}}人</div>
        <div class="seat flex-h">
          <div class="title">座位号：</div>
          <div class="flex-h flex-hw seat_area">
            <div
              v-for="(seat, index) in this.order.SEAT_NO_LIST"
              :key="index"
              class="fontsize32 baseBgColorNoActive"
            >{{seat}}</div>
          </div>
        </div>
      </div>
      <div class="price part">
        <div class="total_prices ">
          <span>票价：</span>￥{{this.order.TICKET_PRICE | priceFilters}}×{{this.order.TAKE_BUS_PEOPLE_COUNT}}
        </div>
        <div class="discount">
          <span>折扣：</span>{{this.order.DISCOUNT_DESC}}
        </div>
        <div class="sum_payable">实付：￥{{this.order.PAY_PRICE | priceFilters}}</div>
      </div>
      <div class="explain">
        <i
          class="iconfont"
          :class="confirmExplain ? 'icon-circle-o' : 'icon-circle'"
          @click="confirmExplain = !confirmExplain"
        ></i>
        我已阅读并同意<span
          @click="goToTicketRule"
          class="baseColorNoAcitve"
        >“购票说明”</span></div>
      <div class="confirm">
        <button
          class="fontsize32"
          @click="getVerifyUserSession()"
        >{{btnText}}</button>
      </div>
    </div>
    <!-- 返回上一页提示用户会清空已锁定的座位 -->
    <div
      class="confirm_screen"
      v-show="confirmState"
    >
      <div class="confirm_area">
        <p>返回后，将不会保留当前选中的座位</p>
        <div class="ctrl_btn">
          <button
            class="cancel fontsize32"
            @click="backToBuyTicket"
          >返回</button>
          <button
            class="buy_ticket fontsize32"
            @click="keepBuy"
          >继续购票</button>
        </div>
      </div>
    </div>
    <!--<div class="pay">
      <div class="pay_area">
        <div class="pay_title flex-h fontsize40">
          <i class="iconfont icon-cancel fontsize48"></i>
          <span>选择支付</span>
          <i></i>
        </div>
        <div class="payment_amount">￥18.00</div>
      </div>
    </div>-->
    <StartPlan
      ref="showStartPlan"
      :startPlanDetail="startPlanDetail"
    ></StartPlan>
  </div>
</template>

<script>
import { getRouteUnlockSeat } from './../../interface/getInterface'
import { priceFilters, stationFilters } from '../../filters/filters'
import { payOrder } from '../../api/api'
import StartPlan from '../../components/buy/StartPlan'
// import ConfirmBox from '../../components/confirm/ConfirmBox.js'
// import { wxLogin } from '../../utils/wxLogin.js'
import { verifyUserSession } from '../../utils/wxLogin.js'
export default {
  components: {
    StartPlan
  },
  data() {
    return {
      toggleList: false, // 切换总站数与具体站名
      order: { // 订单数据，下面为初始化数据
        'DEPART_TIME': '',
        'DISCOUNT': 0,
        'END_STATION': '',
        'END_TIME': 0,
        'OVER_TIME': 0,
        'PAY_PRICE': 0,
        'PRETEND_ORDER_ID': '',
        'SEAT_NO_LIST': [],
        'START_STATION': '',
        'START_TIME': 0,
        'TAKE_BUS_PEOPLE_COUNT': 0,
        'TICKET_DATE_TYPE': 0,
        'TICKET_PRICE': 0,
        'VIA_STATION_COUNT': 0,
        'VIA_STATION_LIST': [
          {
            'ADDRESS': '嘉定长途客运',
            'ADDRESS_LNG': '100.123456',
            'ADDRESS_LAT': '50.123456'
          }
        ]
      }, // 通过缓存读取当前订单数据
      confirmExplain: false, // 是否勾选说明
      wayHeight: 0, // 存储具体站点所在div高度，用于动画效果
      numHeight: '', // 存储总站数所在div高度，用于动画效果
      routeName: '', // 当前线路名
      loadingState: true, // 页面加载中状态
      confirmState: false, // 返回上一页的确认弹窗状态
      countdown: '', // 倒计时定时器
      startPlanDetail: {// 需要传入弹窗组件中的数据
        departTime: '',
        dayList: []
      },
      btnText: '立即支付',
      isShowPopup: true,
      isBack: false // 是否允许返回
    }
  },
  beforeRouteLeave(to, from, next) { // 监听页面离开，当指向上一页时弹出确认框执行对应操作
    if (to.name === 'buyTicket' && this.isShowPopup === true) { // 路由指向上一页
      this.confirmState = true
      if (this.isBack) {
        next()
      } else {
        next(false)
      }
    } else {
      this.confirmState = false
      // clearInterval(this.countdown)
      next()
    }
  },
  filters: {
    priceFilters,
    stationFilters,
    // 倒计时过滤器
    overTimeFilters(val) {
      if (val < 0) {
        return '已过期'
      } else {
        return val + 's'
      }
    },
    // 左上角票类型图片过滤器
    showTheTag(val) {
      if (val === 1) {
        return require('@wechat/img/day-ticket.png')
      } else if (val === 2) {
        return require('@wechat/img/week-ticket.png')
      } else {
        return require('@wechat/img/month-ticket.png')
      }
    }
  },
  mounted() {
    this.order = JSON.parse(window.localStorage.getItem('order')) // 在缓存中读取订单数据
    this.startPlanDetail.departTime = this.order.DEPART_TIME // 将缓存中的DEPART_TIME赋给startPlanDetail对象，并传入弹窗组件
    this.startPlanDetail.dayList = this.order.DAY_LIST// 同上
    this.countdown = setInterval(() => { // 进行订单倒计时，仅作参考用，实际是否超时以后台为准
      this.order.OVER_TIME--
      if (this.order.OVER_TIME < 0) { // 订单已超时的操作
        clearInterval(this.countdown)
        this.showToast('订单已过期')
        this.btnText = '重新购票'
      } else {
        localStorage.setItem('order', JSON.stringify(this.order))
      }
    }, 1000)
    this.routeName = JSON.parse(window.localStorage.getItem('route')).ROUTE_NAME // 在缓存中获取线路名
    let size = parseFloat(document.getElementsByTagName('html')[0].style.fontSize) // 获取当前根字体大小
    this.numHeight = 0.746667 * size + 'px' // 计算站点数div高度
    this.wayHeight = this.order.VIA_STATION_LIST.length * 0.64 * size + 'px' // 计算中间站点div高度
    this.loadingState = false // 关闭加载状态
  },
  methods: {
    buyTicket() { // 确认支付前必须勾选说明，否则当前按钮无效果
      if (this.confirmExplain) {
        payOrder(this.order.PRETEND_ORDER_ID).then(res => {
          let obj = this.$getUrlParams('?' + res.PAYMENT_STR)
          // 拉起微信支付
          this.$WX.then(WX => {
            WX.chooseWXPay(obj.timeStamp, obj.nonceStr, obj.package, obj.signType, obj.paySign, () => {
              // window.location.assign('paySuccess.html?number=' + orderId)
              if (this.countdown) {
                clearInterval(this.countdown)
              }
              this.$router.push({
                name: 'paymentResult',
                query: {
                  ORDER_NO: res.ORDER_NO,
                  direction: this.$route.query.direction
                }
              })
            }, err => {
              console.dir(err)
              this.$toastMsg('支付失败')
            }, () => {
            })
          }).catch(err => {
            console.dir(err)
            console.log('调用支付接口失败')
          })
        })
      } else {
        this.showToast('请阅读并同意《购票说明》')
      }
    },
    goToBuyIndex() {
      this.isShowPopup = false
      this.$router.go(-1)
    },
    getVerifyUserSession() { // 校验用户授权信息是否有效
      verifyUserSession(this, this.selectBtnFn())
    },
    selectBtnFn() { // 根据倒计时选择按钮事件
      if (this.order.OVER_TIME < 0) {
        this.goToBuyIndex()
      } else {
        this.buyTicket()
      }
    },
    backToBuyTicket() { // 确定返回上一页请求接口解锁座位并执行返回上一页的操作
      getRouteUnlockSeat().then(() => {
        clearInterval(this.countdown)
        this.isBack = true
        this.$router.go(-1)
      })
    },
    keepBuy() { // 返回上一页时选择继续购票路由重新指回本页并关闭确认弹窗
      // this.$router.go(1)
      this.confirmState = false
    },
    showStartPlan() {
      this.$refs.showStartPlan.mask.show()
    },
    goToTicketRule() {
      this.$router.push({ name: 'ticketRule' })
    }
  }
}
</script>

<style lang="scss" scoped>
html,
body {
  // overflow: hidden;
}
.create_main {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #f2f5f6;
  // padding-top: 32px;
  box-sizing: border-box;
  font-weight: 400;
  overflow: auto;
  .create_area {
    height: 100%;
    position: relative;
  }
  .part {
    background-color: #fff;
    border-radius: 4px; /*no*/
    margin: 32px;
    margin-top: 0;
  }
  .ticket_info {
    margin-top: 32px;
    position: relative;
    .line_name {
      min-height: 80px;
      padding: 16px 0;
      h3 {
        padding-left: 108px;
        padding-right: 30px;
      }
      p {
        white-space: nowrap;
        padding-right: 32px;
        color: #027aff;
      }
    }
    .line_main {
      position: relative;
      .change_icon {
        width: fit-content;
        height: fit-content;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 32px;
        margin: auto;
        color: #027aff;
      }
      .line_info {
        padding: 0 32px 28px 96px;
        .station {
          height: 44px;
          line-height: 44px;
          padding-left: 64px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .station_list {
          padding: 10px 0;
          padding-left: 64px;
          .station_num {
            overflow: hidden;
            // transition: all 0.5s;
            i {
              height: 56px;
              line-height: 56px;
              display: inline-block;
              color: #027aff;
            }
            span {
              font-size: 28px;
              height: 56px;
              line-height: 56px;
              display: inline-block;
            }
          }
          .station_way {
            overflow: hidden;
            // transition: all 0.5s;
            p {
              font-size: 24px;
              height: 48px;
              line-height: 48px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
        }
        .long_string {
          width: 1px;
          border-left: 4px dashed #ccc;
          height: calc(100% - 48px);
          background-color: transparent;
          position: absolute;
          left: 114px;
          top: 10px;
          i {
            width: 32px;
            text-align: center;
            height: 36px;
            line-height: 36px;
            display: inline-block;
            color: #5c83ff;
            transform: scale(0.8);
            background-color: #fff;
            position: absolute;
            left: -17px;
          }
          .start {
            top: -10px;
          }
          .end {
            bottom: -10px;
          }
        }
      }
    }
    .ticket_time {
      height: 64px;
      line-height: 64px;
      margin: 0 32px;
      border-top: 1px solid #ddd; /*no*/
      span {
        color: #666;
      }
    }
    .tag {
      width: 100px;
      position: absolute;
      top: 0;
      left: 0;
      img {
        width: 100%;
      }
    }
  }
  .passenger {
    padding: 32px;
    .num {
      margin-bottom: 32px;
      span {
        color: #666;
      }
    }
    .seat {
      .title {
        white-space: nowrap;
        color: #666;
      }
      p {
        background-color: #027aff;
        color: #fff;
        border-radius: 4px; /*no*/
        margin-left: 32px;
        margin-bottom: 16px;
        padding: 0 8px;
      }
      .seat_area {
        margin-left: 8px;
        div {
          width: 80px;
          height: 44px;
          margin: 0 8px 16px 8px;
          text-align: center;
          line-height: 44px;
          border-radius: 8px;
          color: #ffffff;
        }
      }
    }
  }
  .price {
    padding: 32px;
    span {
      color: #666;
    }
    .total_prices {
      padding-bottom: 32px;
    }
    .discount {
      padding-bottom: 52px;
    }
    .sum_payable {
      color: #e84d38;
      padding-top: 14px;
      border-top: 1px solid #ddd; /*no*/
    }
  }
  .explain {
    padding: 0 32px;
    height: 48px;
    line-height: 48px;
    margin-bottom: 100px;
    i {
      width: 48px;
      height: 48px;
      line-height: 48px;
      text-align: center;
      display: inline-block;
    }
    span {
      color: #027aff;
    }
    .icon-circle-o {
      color: #027aff;
    }
  }
  .confirm {
    margin: 0 32px;
    button {
      width: 686px;
      height: 72px;
      border: 0;
      background-color: #027aff;
      color: #fff;
      border-radius: 36px; /*no*/
      // position: absolute;
      bottom: 32px;
      transition: all 0.5s;
    }
    .disabled_btn {
      background-color: #ccc;
    }
  }
  .pay {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    .pay_area {
      width: 100%;
      background-color: #fff;
      position: absolute;
      bottom: 0;
      border-radius: 10px 10px 0px 0px; /*no*/
      .pay_title {
        text-align: center;
        height: 112px;
        line-height: 112px;
        border-bottom: 1px solid #eee; /*no*/
        i {
          width: 112px;
          height: 112px;
          display: inline-block;
          color: #ccc;
        }
        span {
          width: 526px;
          display: inline-block;
        }
      }
      .payment_amount {
        height: 116px;
        line-height: 116px;
        text-align: center;
        font-weight: 500;
      }
    }
  }
  .confirm_screen {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 1999;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    .confirm_area {
      width: fit-content;
      height: fit-content;
      background-color: #fff;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
      border-radius: 8px; /*no*/
      p {
        padding: 32px;
      }
      .ctrl_btn {
        display: flex;
        border-top: 2px solid #ccc; /*no*/
        button {
          width: 50%;
          height: 80px;
          border: 0;
          background-color: #fff;
        }
        .cancel {
          border-bottom-left-radius: 8px; /*no*/
          border-right: 2px solid #ccc; /*no*/
        }
        .buy_ticket {
          border-bottom-right-radius: 8px; /*no*/
          color: #027aff;
        }
      }
    }
  }
}
</style>
